package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.AnalysisMysqlQueryRpcService;
import com.hunttown.mes.service.AnalysisMysqlQueryService;
import com.hunttown.common.domain.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * created by wangjunfu on 2019-01-29 16:13:47
 */
@Service
public class AnalysisMysqlQueryRpcServiceImpl implements AnalysisMysqlQueryRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisMysqlQueryRpcServiceImpl.class);

    private final AnalysisMysqlQueryService analysisMysqlQueryService;

    @Autowired
    public AnalysisMysqlQueryRpcServiceImpl(AnalysisMysqlQueryService analysisMysqlQueryService) {
        this.analysisMysqlQueryService = analysisMysqlQueryService;
    }

    @Override
    public Boolean backupTableByDate(String dbName, String toTable, String fromTable, String createTime) {
        try {
            Query query = Query.noPage();

            if (StringUtils.isBlank(dbName)) {
                return false;
            }
            query.put("dbName", dbName);

            if (StringUtils.isBlank(toTable)) {
                return false;
            }
            query.put("toTable", toTable);

            if (StringUtils.isBlank(fromTable)) {
                return false;
            }
            query.put("fromTable", fromTable);

            if (StringUtils.isBlank(createTime)) {
                return false;
            }
            query.put("createTime", createTime);

            analysisMysqlQueryService.backupTableByDate(query);
            return true;

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MysqlQueryRpcService backup table failed.", e);
            throw new RuntimeException("MysqlQueryRpcService backup table failed, Please order your code.", e);
        }
    }

    @Override
    public void deleteInformationByDate(String dbName, String fromTable, String createTime) {
        try {
            Query query = Query.noPage();

            if (StringUtils.isBlank(dbName)) {
                return;
            }
            query.put("dbName", dbName);

            if (StringUtils.isBlank(fromTable)) {
                return;
            }
            query.put("fromTable", fromTable);

            if (StringUtils.isBlank(createTime)) {
                return;
            }
            query.put("createTime", createTime);

            analysisMysqlQueryService.deleteInformationByDate(query);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("MysqlQueryRpcService delete by date failed.", e);
            throw new RuntimeException("MysqlQueryRpcService delete by date failed, Please order your code.", e);
        }
    }
}